package com.mrlee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Account;
import com.mrlee.bean.Service;
import com.mrlee.mapper.AccountMapper;
import com.mrlee.mapper.ServiceMapper;
import com.mrlee.service.AccountService;
import com.mrlee.utils.AjaxResult;
import com.mrlee.utils.StrUtils;
import com.mrlee.utils.TimeUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/13.
 */
@org.springframework.stereotype.Service
public class AccountServiceImpl implements AccountService {


    @Resource
    private AccountMapper accountMapper;

    @Resource
    private ServiceMapper serviceMapper;


    /**
     * 根据Id查找账号
     * @param aid
     * @return
     */
    @Override
    public Account selectbyid(Integer aid) {
        return accountMapper.selectAccountById(aid);
    }

    /**
     * 查找全部账号并截取月份
     * @return
     */
    @Override
    public List<Account> selectallacc() {
        List<Account> accList = accountMapper.selectAllAccount();
        for (int i = 0; i < accList.size(); i++) {
            accList.get(i).setCreateDate((accList.get(i).getCreateDate()).substring(0,7));
        }
        return accList;
    }


    /**
     * 查找全部账号
     * @return
     */
    @Override
    public AjaxResult selectAllAccount() {
        AjaxResult result = new AjaxResult();
        List<Account> accountList = accountMapper.selectAllAccount();
        if (accountList == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(accountList);
        return result;
    }


    /**
     * 修改账号状态
     * @param accountId 账号Id
     * @param status 账号当前状态
     * @return
     */
    @Override
    public AjaxResult changeStatus(Integer accountId, Integer status) {
        AjaxResult result = new AjaxResult();
        Integer newStatus = null;
        //1代表开通状态  2代表暂停状态  0代表删除状态
        //status代表当前状态,newStatus代表改变后的状态
        if (status == 1) {
            newStatus = 2;
            List<Service> serviceIdList = serviceMapper.selectSeviceByAccountId(accountId);
            for (Service service : serviceIdList){
                int serviceId = service.getServiceId();
                serviceMapper.updatePauseDate(serviceId, TimeUtils.time());
                Integer num = serviceMapper.updateStatus(serviceId, newStatus);
                if (num < 1) {
                    result.setCode("0");
                    result.setMessage("更改失败");
                    return result;
                }
            }
            accountMapper.updatePauseDate(accountId, TimeUtils.time());
        }
        if (status == 2) {
            newStatus = 1;
            accountMapper.updatePauseDate(accountId, null);
        }
        if (status == 0) {
            newStatus = 0;
            accountMapper.updateCloseDate(accountId,TimeUtils.time());
        }

        Integer num = accountMapper.updateStatus(accountId, newStatus);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("更改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("更改成功");
        return result;
    }

    /**
     * 添加账号
     * @param account 账号信息
     * @return
     */
    @Override
    public AjaxResult addAccount(Account account) {
        AjaxResult result = new AjaxResult();
        boolean isTel = StrUtils.isPhoneNumberRight(account.getTelephone());
        boolean isCardId = StrUtils.isCardId(account.getIdcardNo());
        boolean isName = StrUtils.isChineseLetterNumRight(account.getRealName());
        boolean isLoginname = StrUtils.isLetterNum_Right(account.getLoginName());
        boolean isPassword = StrUtils.isLetterNum_Right(account.getLoginPasswd());
        Account accountselect = accountMapper.selectAccountByCardId(account.getIdcardNo());

        Account accountloginname = accountMapper.selectAccountByLoginName(account.getLoginName());
        if (accountloginname != null) {
            result.setCode("0");
            result.setMessage("登录账号已存在");
            return result;
        }
        if (accountselect != null) {
            result.setCode("0");
            result.setMessage("身份证已注册");
            return result;
        }
        if (!("".equals(account.getEmail()))) {
            boolean isEmail = StrUtils.isMailRight(account.getEmail());
            if (account.getEmail().length() > 50) {
                isEmail = false;
            }
            if (isEmail == false) {
                result.setCode("0");
                result.setMessage("邮箱不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getRecommenderId()))) {
            boolean isRecomCardId = StrUtils.isCardId(account.getRecommenderId());
            if (isRecomCardId == false) {
                result.setCode("0");
                result.setMessage("推荐人身份证号不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getMailaddress()))) {
            if (account.getMailaddress().length() > 50) {
                result.setCode("0");
                result.setMessage("地址长度不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getZipcode()))) {
            boolean isZipcode = StrUtils.isNumber(account.getZipcode());
            if (account.getZipcode().length() != 6) {
                isZipcode = false;

            }
            if (isZipcode == false) {
                result.setCode("0");
                result.setMessage("邮编不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getQq()))) {
            boolean isQq = StrUtils.isNumber(account.getQq());
            if (account.getQq().length() < 5 || account.getQq().length() > 13) {
                isQq = false;
            }
            if (isQq == false) {
                result.setCode("0");
                result.setMessage("QQ不符合要求");
                return result;
            }
        }
        if (!(isTel && isCardId && isLoginname && isName && isPassword && (account.getLoginName().length() < 20) && (account.getRealName().length() < 30) && (account.getLoginPasswd().length() < 30))) {
            result.setCode("0");
            result.setMessage("必填信息不符合要求");
            return result;
        }

        account.setStatus("1");
        account.setCreateDate(TimeUtils.time());
        account.setBirthdate(TimeUtils.catBirthday(account.getIdcardNo()));

        int num = accountMapper.insert(account);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("添加成功");
        return result;
    }

    /**
     * 根据Id查找账号
     * @param accountId 账号Id
     * @return
     */
    @Override
    public AjaxResult selectAccountById(Integer accountId) {
        AjaxResult result = new AjaxResult();
        Account account = accountMapper.selectAccountById(accountId);

        if (account == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        } else {
            Account recommenderaccount = accountMapper.selectAccountByCardId(account.getRecommenderId());
            if (recommenderaccount != null) {
                int recommenderid = recommenderaccount.getAccountId();
                account.setRecomenderid(recommenderid);
            }
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(account);
        }
        return result;
    }


    /**
     * 根据登录名查找账号
     * @param accountname 登录名
     * @return
     */
    @Override
    public AjaxResult selectAccountByAccountName(String accountname) {
        AjaxResult result = new AjaxResult();
        Account account = accountMapper.selectAccountByLoginName(accountname);
        if (account == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(account);
        return result;
    }

    /**
     * 根据身份证号查找账号
     * @param cardId
     * @return
     */
    @Override
    public AjaxResult selectAccountByCardId(String cardId) {
        AjaxResult result = new AjaxResult();
        Account account = accountMapper.selectAccountByCardId(cardId);
        if (account == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(account);
        return result;
    }

    /**
     * 修改账号
     * @param account 账号信息
     * @return
     */
    @Override
    public AjaxResult updateAccount(Account account) {
        AjaxResult result = new AjaxResult();
        boolean isTel = StrUtils.isPhoneNumberRight(account.getTelephone());
        boolean isCardId = StrUtils.isCardId(account.getIdcardNo());
        boolean isName = StrUtils.isChineseLetterNumRight(account.getRealName());
        boolean isLoginname = StrUtils.isLetterNum_Right(account.getLoginName());



        if (!("".equals(account.getEmail()))) {
            boolean isEmail = StrUtils.isMailRight(account.getEmail());
            if (account.getEmail().length() > 50) {
                isEmail = false;
            }
            if (isEmail == false) {
                result.setCode("0");
                result.setMessage("邮箱不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getRecommenderId()))) {
            boolean isRecomCardId = StrUtils.isCardId(account.getRecommenderId());
            if (isRecomCardId == false) {
                result.setCode("0");
                result.setMessage("推荐人身份证号不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getMailaddress()))) {
            if (account.getMailaddress().length() > 50) {
                result.setCode("0");
                result.setMessage("地址长度不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getZipcode()))) {
            boolean isZipcode = StrUtils.isNumber(account.getZipcode());
            if (account.getZipcode().length() != 6) {
                isZipcode = false;

            }
            if (isZipcode == false) {
                result.setCode("0");
                result.setMessage("邮编不符合要求");
                return result;
            }
        }
        if (!("".equals(account.getQq()))) {
            boolean isQq = StrUtils.isNumber(account.getQq());
            if (account.getQq().length() < 5 || account.getQq().length() > 13) {
                isQq = false;
            }
            if (isQq == false) {
                result.setCode("0");
                result.setMessage("QQ不符合要求");
                return result;
            }
        }
        if (!(isTel && isCardId && isLoginname && isName && (account.getLoginName().length() < 20) && (account.getRealName().length() < 30))) {
            result.setCode("0");
            result.setMessage("必填信息不符合要求");
            return result;
        }


        int num = accountMapper.updateByPrimaryKeySelective(account);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 搜索
     * @param name 姓名
     * @param cardid 身份证号
     * @param status 状态
     * @param loginname 登录名
     * @return
     */
    @Override
    public AjaxResult search(String name, String cardid, String status, String loginname) {
        AjaxResult result = new AjaxResult();

        List<Account> accountList = new ArrayList<Account>();
         if (!("".equals(cardid))) {
            Account account = accountMapper.selectAccountByCardId(cardid);
            accountList.add(account);
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(accountList);
            return result;
        }
        if (!("".equals(loginname))) {
            List<Account> accounts = accountMapper.searchAccountByLoginName(loginname);
            for (Account account : accounts) {
                System.out.println(account);
            }
            accountList=accounts;
            result.setCode("1");
            result.setMessage("查询成功");
            result.setData(accountList);
            return result;
        }
        if ("全部".equals(status)) {
            if ("".equals(name)) {
                accountList = accountMapper.selectAllAccount();
            } else if (!("".equals(name))) {
                accountList = accountMapper.selectAccountByName(name);
            }
        } else {
            String newstatus = null;
            if ("开通".equals(status)){
                newstatus = "1";
            }else if ("暂停".equals(status)){
                newstatus = "2";
            }else if ("删除".equals(status)){
                newstatus = "0";
            }
            List<Account> accounts = accountMapper.selectAccountByStatus(newstatus);
            if ("".equals(name)) {
                accountList = accounts;
            } else {
               List<Account> accountListByName = accountMapper.selectAccountByName(name);
                for (Account account : accounts) {
                    for (Account account1 : accountListByName) {
                        if (account1.getRealName().equals(account.getRealName())){
                            accountList.add(account1);
                        }
                    }
                }
            }

        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(accountList);
        return result;
    }

    /**
     * 动态搜索
     * @param name 姓名
     * @param cardid 身份证号
     * @param status 状态
     * @param loginname 登录名
     * @return
     */

    @Override
    public AjaxResult dynamicSearch(String name, String cardid, String status, String loginname) {


        if ("开通".equals(status)){
            status = "1";
        }else if ("暂停".equals(status)){
            status = "2";
        }else if ("删除".equals(status)){
            status = "0";
        }else {
            status = null;
        }

        name = name.isEmpty()? null :name;
        cardid = cardid.isEmpty()? null :cardid;
        loginname = loginname.isEmpty()? null :loginname;

        List<Account> accountList = accountMapper.dynamicSearch(name,cardid,status,loginname);
        AjaxResult result = new AjaxResult();
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(accountList);
        return result;
    }

    /**
     * 分页
     * @param pageNo 当前页
     * @param pageSize 每页显示信息数
     * @return
     */
    @Override
    public PageInfo<Account> queryAccountByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo,pageSize);
        List<Account> accountList = accountMapper.selectAllAccount();
        PageInfo<Account> accountPageInfo = new PageInfo<Account>(accountList);
        return accountPageInfo;
    }


    @Override
    public PageInfo<Account> queryAccountByPageb(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo,pageSize);
        List<Account> accList = accountMapper.selectAllAccount();
        for (int i = 0; i < accList.size(); i++) {
            accList.get(i).setCreateDate((accList.get(i).getCreateDate()).substring(0,7));
        }
        PageInfo<Account> accountPageInfo = new PageInfo<Account>(accList);
        return accountPageInfo;
    }

    @Override
    public PageInfo<Account> queryAccountByPager(Integer pageNo, Integer pageSize) {
        List<Account> accList = accountMapper.selectAllAccount();
        for (int i = 0; i < accList.size(); i++) {
            accList.get(i).setCreateDate((accList.get(i).getCreateDate()).substring(0,7));
        }
        PageInfo<Account> accountPageInfo = new PageInfo<Account>(accList);
        return accountPageInfo;
    }
}
